package com.xjf.bookshelf;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private BookImageView firstBook;
    private TextView tvBook;
    private int bookWidth;
    private int view2left;
    private LinearLayoutManager linearLayoutManager;

    private int[] imgs = {R.mipmap.img_book_1, R.mipmap.img_book_2, R.mipmap.img_book_3, R.mipmap.img_book_4};
    private String[] bookName = {"西游记", "水浒传", "红楼梦", "三国演义"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.recycle_view);
        firstBook = findViewById(R.id.first_book);
        tvBook = findViewById(R.id.tv_book);
        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(new MyAdapter());
        setFirstView(new Book(imgs[0], bookName[0]),true);
        bookWidth = (int) getResources().getDimension(R.dimen.book_item_width);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView r, int newState) {
                super.onScrollStateChanged(r, newState);
                //如果停止滑动的时候第一本书被覆盖则滑动到不被覆盖为止
                if (view2left % bookWidth != 0) {
                    //判断是往左滑动还是往右
                    if (bookWidth * 2.5 - view2left < 0) {
                        recyclerView.smoothScrollBy(view2left - bookWidth * 3, 0);
                    } else {
                        //故意加1个dp，原因是如果是左滑到某个item置顶，其实第0个view没被回收，有的事件没有被触发
                        recyclerView.smoothScrollBy(view2left - bookWidth * 2+1, 0);
                    }
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                setViewWidth();
            }
        });

    }

    private void setFirstView(final Book book,boolean isTop) {
        firstBook.setBook(book);
        if (isTop) {
            tvBook.setText(book.getName());
        }
    }


    private void setViewWidth() {
        int count = recyclerView.getChildCount();
        //获取下标为2的view
        View view2 = recyclerView.getChildAt(2);
        //获取该view的位置(左上角的坐标)，第一本书是第二本书的两倍大
        view2left = view2.getLeft();
        if (view2left > bookWidth * 3) {
            //理论上讲第三本书的左上角坐标不会超过3倍书的大小，因为超过了，他就不是第三本书而是第四本，
            // 但是，因为recycleView的回收和新增view的时间延迟上的问题会出现超过的情况，
            // 我们又是通过这个距离来改变前两本书的大小，所以这个值最大只能是bookWidth * 3
            view2left = bookWidth * 3;
        } else if (view2left < bookWidth * 2) {
            view2left = bookWidth * 2;
        }
        boolean isTop = (view2left>bookWidth*2.8);
        for (int i = 0; i < count; i++) {
            BookImageView imgBook = recyclerView.getChildAt(i).findViewById(R.id.img_book);
            //停止滑动的时候第一本书不需要隐藏，否则隐藏第一本,第一本书的宽度是不断变小的，第二本书的宽度是不断变大的，
            //根据业务需求，第一本书必须隐藏，这个地方有个坑，如果xml设置为margin而不是padding的话，会影响第一个view的回收，
            // 造成需求上应该被隐藏的view下标被加一，变成不隐藏了，
            if (view2left%bookWidth==0){
                //单第一本书刚好被滚动出去的时候，第二本书变成第一本书，这个时候第一本书是不能被隐藏的，否则点击事件就没了
                imgBook.setVisibility(View.VISIBLE);
            }else {
                //其他情况下第一本书是处于变小的状态的是要隐藏的
                imgBook.setVisibility(i == 0 ? View.INVISIBLE : View.VISIBLE);
            }
            if (i == 0) {
                //更新recycleView下面的ImageView的内容
                setFirstView(imgBook.getBook(),isTop);
            }
            LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) imgBook.getLayoutParams();
            //定义view的宽度
            int width;
            //根据view的下标，以及下标为2的view的位置动态修改，下标为0和1的宽度
            if (i == 1) {
                width = bookWidth * 4 - view2left;
            } else if (i == 0) {
                width = view2left - bookWidth;
            } else {
                width = bookWidth;
            }
            linearParams.width = width;
            imgBook.setLayoutParams(linearParams);
        }
        //滑动过程中第一本书的动画效果，改变margins
        LinearLayout.LayoutParams firstBookLinearParams = (LinearLayout.LayoutParams) firstBook.getLayoutParams();
        firstBookLinearParams.setMargins((int) ((bookWidth * 3 - view2left)*0.15 ),
                (int) ((bookWidth * 3 - view2left) * 0.1 ),
                (int) ((bookWidth * 3 - view2left) * 0.15 ),
                (int) ((bookWidth * 3 - view2left) * 0.2 ));
        firstBook.setLayoutParams(firstBookLinearParams);

    }


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_book, parent, false);
            return new ViewHolder(view);
        }


        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.img_book.setBook(new Book(imgs[position % 4], bookName[position%4]));
        }

        @Override
        public int getItemCount() {
            return 2000;
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            BookImageView img_book;

            public ViewHolder(View itemView) {
                super(itemView);
                img_book = itemView.findViewById(R.id.img_book);
            }
        }
    }
}
